package com.example.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.example.room.user.User;
import com.example.xc.R;

import java.util.List;

/**
 * 同学适配器
 */
public class StudentListAdapter extends RecyclerView.Adapter<StudentListAdapter.ViewHolder> {

    private List<User> desks;

    FragmentActivity activity;

    /***
     * @param activity 传入Activity对象
     */
    public StudentListAdapter(FragmentActivity activity, List<User> deskList) {
        this.activity = activity;
        desks = deskList;
    }

    /**
     * 用于创建选项布局
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //关联列表选项布局
        View view = LayoutInflater.from
                (parent.getContext()).inflate(R.layout.new_item, parent, false);
        ViewHolder holder = new ViewHolder(view);  //调用内部类ViewHolder

        return holder;
    }

    /**
     * 用于绑定选项数据
     */
    @SuppressLint("DefaultLocale")
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder,
                                 @SuppressLint("RecyclerView") final int position) {
        User desk = desks.get(position);
        holder.tvPeople.setText(String.format("姓名： %s", desk.getXingming()));
        holder.tvPwd.setText(String.format("班级： %s", desk.getBanji()));
        holder.tvXuaHao.setText(String.format("学号： %s", desk.getNumber()));
    }

    /***
     *  用于设置选项数量
     */
    @Override
    public int getItemCount() {
        return desks.size();                               //获得图片数组长度
    }

    /**
     * 内部类
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvPeople;
        TextView tvPwd;
        TextView tvXuaHao;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            //获得列表选项图片控件
            tvPeople = itemView.findViewById(R.id.desk_people);//获得列表选项文本控件
            tvPwd = itemView.findViewById(R.id.desk_pwd);
            tvXuaHao = itemView.findViewById(R.id.xuehao);
        }
    }
}
